package de.g18.it0a.jlaser.nativ.menu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import de.g18.it0a.jlaser.dataio.RoomConfigurationEditor;
import de.g18.it0a.jlaser.dataio.RoomConfigurationReader;
import de.g18.it0a.jlaser.dataio.TestDataProvider;
import de.g18.it0a.jlaser.domain.Position;
import de.g18.it0a.jlaser.domain.RoomConfiguration;
import de.g18.it0a.jlaser.exception.InitializationException;
import de.g18.it0a.jlaser.exception.JLFMalformedFileException;
import de.g18.it0a.jlaser.main.NativApplicationStarter;
import de.g18.it0a.jlaser.nativ.menu.util.MenuPrinter;

public enum RoomEditMenuEntry implements MenuEntry {

	BACK("Zurück"), NEW("Neu"), LOAD_TEST_ROOM("Test Raum laden"), IMPORT(
			"Raum importieren"), EXPORT("Raum exportieren"), SHOW("Anzeigen"), ADD_VETRINE(
			"Vetrine hinzufügen"), REMOVE_VETRINE("Vetrine entfernen");

	private final String text;

	private RoomEditMenuEntry(String aText) {
		text = aText;
	}

	@Override
	public String getText() {
		return text;
	}

	@Override
	public int getOrdinal() {
		return ordinal();
	}

	public static void printRoomEditMenu() {
		RoomEditMenuEntry menuEntry = null;
		do {
			do {
				System.out.println("## Raum bearbeiten");
				MenuPrinter.getInstance().print(RoomEditMenuEntry.class);

				int selection = -1;
				try {
					System.out.print("Auswahl: ");
					Scanner inputScanner = new Scanner(System.in);
					selection = inputScanner.nextInt();
				} catch (InputMismatchException e) {
					System.out.print("Fehlerhafte Eingabe!");
				}
				System.out.print("\n");
				menuEntry = getByOrdinal(selection);
			} while (menuEntry == null);

			switch (menuEntry) {
			case BACK:
				NativApplicationStarter.printMainMenu();
				break;

			case NEW:
				newRoom();
				break;

			case LOAD_TEST_ROOM:
				loadRoomConfigFile();
				break;

			case IMPORT:
				importJlf();
				break;

			case EXPORT:
				exportJlf();
				break;

			case SHOW:
				printRoom();
				break;

			case ADD_VETRINE:
				addVitrine();
				break;

			case REMOVE_VETRINE:
				removeVitrine();
				break;
			}
		} while (menuEntry != BACK);
	}

	public static RoomEditMenuEntry getByOrdinal(int aOrdinal) {
		for (RoomEditMenuEntry entry : values()) {
			if (entry.ordinal() == aOrdinal) {
				return entry;
			}
		}
		return null;
	}

	public static void newRoom() {
		do {
			System.out.println("\n\n\n## Neuer Raum");
			int width = -1;
			do {
				try {
					System.out.print("Breite: ");
					Scanner inputScanner = new Scanner(System.in);
					width = inputScanner.nextInt();
				} catch (InputMismatchException e) {
					System.out.print("Fehlerhafte Eingabe!");
				}
				System.out.print("\n");
			} while (width < 0);

			int height = -1;
			do {
				try {
					System.out.print("Höhe: ");
					Scanner inputScanner = new Scanner(System.in);
					height = inputScanner.nextInt();
				} catch (InputMismatchException e) {
					System.out.print("Fehlerhafte Eingabe!");
				}
				System.out.print("\n");
			} while (height < 0);

			try {
				NativApplicationStarter.roomConfig = new RoomConfiguration(
						width, height);
			} catch (InitializationException e) {
				NativApplicationStarter.roomConfig = null;
				System.out.println("Fehler! " + e.getLocalizedMessage()
						+ "\nBitte versuchen sie es erneut.");
			}
		} while (NativApplicationStarter.roomConfig == null);
	}

	public static void removeVitrine() {
		NativApplicationStarter.checkIfRoomConfigExists();
		RoomConfigurationEditor roomEditor = new RoomConfigurationEditor();
		roomEditor.setRoomConfiguration(NativApplicationStarter.roomConfig);

		Scanner inputScanner = new Scanner(System.in);
		System.out.println("X-Koordinate eingeben: ");
		int xCordR = inputScanner.nextInt();
		System.out.println("Y-Koordinate eingeben: ");
		int yCordR = inputScanner.nextInt();

		Position newVitrinePositionR = new Position(xCordR, yCordR);

		if (roomEditor.getRoomConfiguration().getVetrinePositions()
				.contains(newVitrinePositionR)) {
			if (roomEditor.getRoomConfiguration().getVetrinePositions()
					.remove(newVitrinePositionR)) {
				System.out.println(MessageFormat.format(
						"Die Vitrine {0}, {1} wurde erfolgreich entfernt",
						xCordR, yCordR));
			}
		} else {
			System.out
					.println("Es konnte keine Vitrine an dieser Position gefunden werden!");
		}

		roomEditor.listVitrinePositions();
	}

	public static void addVitrine() {
		NativApplicationStarter.checkIfRoomConfigExists();
		RoomConfigurationEditor roomEditor = new RoomConfigurationEditor();
		roomEditor.setRoomConfiguration(NativApplicationStarter.roomConfig);

		Scanner inputScanner = new Scanner(System.in);
		System.out.println("X-Koordinate eingeben: ");
		int xCord = inputScanner.nextInt();
		System.out.println("Y-Koordinate eingeben: ");
		int yCord = inputScanner.nextInt();

		Position newVitrinePosition = new Position(xCord, yCord);
		if (roomEditor.getRoomConfiguration().isValidTilePosition(
				newVitrinePosition)) {
			while (roomEditor.getRoomConfiguration().getVetrinePositions()
					.contains(newVitrinePosition)) {
				System.out
						.println("Auf dieser Position liegt schon eine Vitrine");
				System.out.println("Zum abbrechen 0 drücken, weiter mit 1");
				if (inputScanner.nextInt() == 0) {
					break;
				}
				System.out.println("X-Koordinate eingeben: ");
				xCord = inputScanner.nextInt();
				System.out.println("Y-Koordinate eingeben: ");
				yCord = inputScanner.nextInt();
				newVitrinePosition = new Position(xCord, yCord);
			}
			roomEditor.setVitrinesInRoomConfig(newVitrinePosition);
		}

		roomEditor.listVitrinePositions();
	}

	public static void exportJlf() {
		do {
			System.out
					.println("Geben Sie den Pfad für die zu exportierende Datei an.");
			System.out.println("z.B. C:\\temp\\test.jlf");
			System.out.print("Eingabe Pfad: ");
			Scanner inputScanner = new Scanner(System.in);
			String pfad = inputScanner.next();

			if (NativApplicationStarter.roomConfig != null) {
				RoomConfigurationEditor rce = new RoomConfigurationEditor();
				rce.setRoomConfiguration(NativApplicationStarter.roomConfig);
				try {
					rce.saveConfigFile(pfad);
				} catch (IOException e) {
					System.err
							.println("Fehler! Raum konnte nicht gespeichert werden.\n"
									+ e.getLocalizedMessage());
				}
			}
		} while (NativApplicationStarter.roomConfig == null);
	}

	public static void importJlf() {
		do {
			System.out
					.println("Geben Sie den Pfad für die zu importierende Datei an.");
			System.out.println("z.B. C:\\temp\\test.jlf");
			System.out.print("Eingabe Pfad: ");
			Scanner inputScanner = new Scanner(System.in);
			String pfad = inputScanner.next();

			try {
				NativApplicationStarter.roomConfig = RoomConfigurationReader
						.read(pfad);
			} catch (FileNotFoundException e) {
				System.out.println("Datei konnte nicht importiert werden...!");
				NativApplicationStarter.roomConfig = null;
				break;
			} catch (JLFMalformedFileException e) {
				System.out.println("Die Datei enthält keine gültigen Daten!\n"
						+ e);
				break;
			}
		} while (NativApplicationStarter.roomConfig == null);
	}

	public static void printRoom() {
		if (NativApplicationStarter.roomConfig != null) {
			
			
			List<Position> listVitrine = NativApplicationStarter.roomConfig
					.getVetrinePositions();
			//200 200 , 300 300
			if (listVitrine != null) {
				for (Position p : listVitrine) {
					System.out.print("Vitrine:");
					System.out.println(p.getX() + "," + p.getY() + "," + (p.getX() + 100) + "," + (p.getY() + 100));
				}
			} else {
				System.out.println("Keine Vitrinen vorhanden!");
			}
		} else {
			System.out.println("Es wurde kein Raum geladen!");
		}
	}

	public static void loadRoomConfigFile() {
		File[] testConfigFiles = TestDataProvider.getInstance()
				.getConfigFiles();
		System.out.println(MessageFormat.format(
				"Es stehen {0} Dateien zur Auswahl.", testConfigFiles.length));
		for (int i = 0; i < testConfigFiles.length; i++) {
			System.out.println(MessageFormat.format("# {0}. {1}", i,
					testConfigFiles[i].getName()));
		}
		Scanner inputScanner = new Scanner(System.in);
		int choice = inputScanner.nextInt();
		File fileToLoad = testConfigFiles[choice];
		System.out.println("Sie haben folgende Datei ausgewählt: "
				+ fileToLoad.getName());
		System.out.println("Datei wird geladen...");
		try {
			NativApplicationStarter.roomConfig = RoomConfigurationReader
					.read(fileToLoad);
		} catch (FileNotFoundException e) {
			System.out.println("Datei konnte nicht gefunden werden...!");
			NativApplicationStarter.roomConfig = null;
		} catch (JLFMalformedFileException e) {
			System.out.println("Die Datei enthält keine gültigen Daten!\n" + e);
		}
	}

}